package com.example.basedemosynchronize;

/**
 * <p>synchronized关键字的作用：能够保证同一时刻最多只有一个线程执行该段代码，保证并发下任务的安全执行
 * synchronized的用法：第一类用法是当作对象锁来用
 * 第二类用法是当作类锁来用
 * </p>
 *
 * @author:libaoguang
 * @date:Creatrd in 2020/3/25
 * @since:1.0
 */
public class Usage2 implements Runnable {
    static Usage2 usage1 = new Usage2();
    static Usage2 usage2 = new Usage2();

    /**
     * <p>
     * </p>
     *
     * @Param:
     * @Return:
     **/
    public  void method1() {
        synchronized (this){
            System.out.println("my name is===" + Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("end===" + Thread.currentThread().getName());
        }
    }


    @Override
    public void run() {
        method1();
    }

    public static void main(String[] args) {
        new Thread(usage1).start();
        new Thread(usage2).start();
    }
}
